package com.beemans.corekit.ui.fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.anythink.core.api.ATAdInfo;
import com.beemans.corekit.R;
import com.beemans.corekit.constant.AdvertConstants;
import com.beemans.corekit.databinding.FragmentToponNativeBinding;
import com.beemans.corekit.integration.toolkit.ToastUtils;
import com.beemans.corekit.integration.topon.ATAdInfoExt;
import com.beemans.corekit.integration.topon.INativeSelfRender;
import com.beemans.corekit.integration.topon.NativeAdvert;
import com.beemans.corekit.integration.topon.NativeAdvertCallback;
import com.beemans.corekit.integration.topon.NativeAdvertConfig;
import com.beemans.corekit.ui.dialog.LoadingDialog;
import com.beemans.corekit.ui.view.CustomNativeRender;
import com.beemans.corekit.utils.TopOnUtils;

public class TopOnNativeFragment extends BaseFragment<FragmentToponNativeBinding> {
    @Nullable
    @Override
    protected FragmentToponNativeBinding getViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        return FragmentToponNativeBinding.inflate(inflater, container, false);
    }

    @Override
    protected void initViews() {
    }

    @Override
    protected void initEvents() {
        binding.nativeBtnLoadAd.setOnClickListener(v -> preloadAdvert());

        binding.nativeBtnShowAd.setOnClickListener(v -> showAdvert());
    }

    private void preloadAdvert() {
        LoadingDialog.showLoading();

        final int[] advertSize = getSelfNativeSize();
        final NativeAdvertConfig advertConfig = new NativeAdvertConfig(AdvertConstants.TP_NATIVE_ID, advertSize[0], advertSize[1]);
        TopOnUtils.loadNativeAdvert(this, advertConfig, null, null, getAdvertCallback());
    }

    private void showAdvert() {
        LoadingDialog.showLoading();

        final int[] advertSize = getSelfNativeSize();
        final NativeAdvertConfig advertConfig = new NativeAdvertConfig(AdvertConstants.TP_NATIVE_ID, advertSize[0], advertSize[1]);
        advertConfig.setShowAfterLoaded(true);
        advertConfig.setPreloadAdvert(true);

        final INativeSelfRender nativeSelfRender = new CustomNativeRender(binding.nativeAdViewSelfRender.getRoot());
        TopOnUtils.loadNativeAdvert(this, advertConfig, binding.nativeFlAd, nativeSelfRender, getAdvertCallback());
    }

    @NonNull
    private NativeAdvertCallback getAdvertCallback() {
        return new NativeAdvertCallback() {
            @Override
            public void onAdvertRequestPre(@NonNull NativeAdvert nativeAdvert, @NonNull ATAdInfoExt adInfoExt) {
                printLog("广告开始请求");
            }

            @Override
            public void onAdvertRequestAlt(@NonNull NativeAdvert nativeAdvert, @NonNull ATAdInfoExt adInfoExt) {
                printLog("广告请求成功");
            }

            @Override
            public void onAdvertLoaded(@NonNull NativeAdvert nativeAdvert) {
                printLog("广告加载成功");
                LoadingDialog.hideLoading();
            }

            @Override
            public void onAdvertLoadFail(@NonNull NativeAdvert nativeAdvert, @Nullable String adError) {
                printLog("广告加载失败");
                LoadingDialog.hideLoading();
            }

            @Override
            public void onAdvertShow(@NonNull NativeAdvert nativeAdvert, @Nullable ATAdInfo atAdInfo) {
                printLog("广告展示 --- atAdInfo:" + atAdInfo);
            }

            @Override
            public void onAdvertShowFail(@NonNull NativeAdvert nativeAdvert) {
                printLog("广告展示失败");
            }

            @Override
            public void onAdvertClicked(@NonNull NativeAdvert nativeAdvert, @Nullable ATAdInfo atAdInfo) {
                printLog("广告点击 --- atAdInfo:" + atAdInfo);
            }

            @Override
            public void onAdvertClose(@NonNull NativeAdvert nativeAdvert, @Nullable ATAdInfo atAdInfo) {
                printLog("广告关闭 --- atAdInfo:" + atAdInfo);
            }

            @Override
            public void onAdvertRevenue(@NonNull NativeAdvert nativeAdvert, @Nullable ATAdInfo atAdInfo) {
                printLog("广告收益 --- atAdInfo:" + atAdInfo);
            }
        };
    }

    @NonNull
    private int[] getSelfNativeSize() {
        return new int[]{
                getResources().getDimensionPixelSize(R.dimen.sw_dp_375),
                getResources().getDimensionPixelSize(R.dimen.sw_dp_181)
        };
    }

    private void printLog(String msg) {
        Log.e("susu", msg);
        ToastUtils.showShort(msg);
    }
}